/*******************************************************************************
 * Copyright 2011, 2012 Chris Banes.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************//*
package com.handmark.pulltorefresh.samples;

import hao.wei.Adapter.AdvertisementAdapter;
import hao.wei.Adapter.CommonUtil;
import hao.wei.domain.Advertisement;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.szkj.mobiletoken.net.NetUtil;

public final class PullToRefreshGridActivity extends Activity {

	static final int MENU_SET_MODE = 0;

	private LinkedList<Advertisement> mListItems;
	private  LinkedList<Advertisement> list;
	private PullToRefreshGridView mPullRefreshGridView;
	private GridView mGridView;
	private AdvertisementAdapter adapter;

	*//** Called when the activity is first created. *//*
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ptr_grid);

		mPullRefreshGridView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
		mGridView = mPullRefreshGridView.getRefreshableView();

		// Set a listener to be invoked when the list should be refreshed.
		mPullRefreshGridView
				.setOnRefreshListener(new OnRefreshListener2<GridView>() {

					@Override
					public void onPullDownToRefresh(
							PullToRefreshBase<GridView> refreshView) {
						Toast.makeText(PullToRefreshGridActivity.this,
								"Pull Down!", Toast.LENGTH_SHORT).show();
						new GetDataTask().execute();
					}

					@Override
					public void onPullUpToRefresh(
							PullToRefreshBase<GridView> refreshView) {
						Toast.makeText(PullToRefreshGridActivity.this,
								"Pull Up!", Toast.LENGTH_SHORT).show();
						new GetDataTask().execute();
					}

				});

		setList();

		TextView tv = new TextView(this);
		tv.setGravity(Gravity.CENTER);
		tv.setText("Empty View, Pull Down/Up to Add Items");
		mPullRefreshGridView.setEmptyView(tv);

		// mAdapter = new ArrayAdapter<String>(this,
		// android.R.layout.simple_list_item_1, mListItems);
		
	}

	private void setList() {
		CommonUtil.showProgressDialog(PullToRefreshGridActivity.this);
		new Thread(new Runnable() {

			@Override
			public void run() {
				 list = NetUtil.getSingleNetUtil()
						.getAdvertisement("166795605675", "0");
				CommonUtil.dismissProgressDialog();
				handler.sendEmptyMessage(11);
			}
		}).start();

	}

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			mListItems = list;
			adapter = new AdvertisementAdapter(PullToRefreshGridActivity.this,mListItems);
			mGridView.setAdapter(adapter);

		}
	};

	private class GetDataTask extends
			AsyncTask<Void, Void, LinkedList<Advertisement>> {

		@Override
		protected LinkedList<Advertisement> doInBackground(Void... params) {
			// Simulates a background job.
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
			}
			return mListItems;
		}

		@Override
		protected void onPostExecute(LinkedList<Advertisement> result) {
			adapter.notifyDataSetChanged();
			mPullRefreshGridView.onRefreshComplete();

			super.onPostExecute(result);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(
				0,
				MENU_SET_MODE,
				0,
				mPullRefreshGridView.getMode() == Mode.BOTH ? "Change to MODE_PULL_DOWN"
						: "Change to MODE_PULL_BOTH");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		MenuItem setModeItem = menu.findItem(MENU_SET_MODE);
		setModeItem
				.setTitle(mPullRefreshGridView.getMode() == Mode.BOTH ? "Change to MODE_PULL_FROM_START"
						: "Change to MODE_PULL_BOTH");

		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_SET_MODE:
			mPullRefreshGridView
					.setMode(mPullRefreshGridView.getMode() == Mode.BOTH ? Mode.PULL_FROM_START
							: Mode.BOTH);
			break;
		}

		return super.onOptionsItemSelected(item);
	}

}
*/